package com.yonxao.demo.crud.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonxao.demo.crud.dao.StudentMapper;
import com.yonxao.demo.crud.entity.Student;
import com.yonxao.demo.crud.pojo.query.ListQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 学生 service impl
 *
 * @author xiaosaguo
 * @date 2021/03/15
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Override
    public Student getById(@NotNull @Min(1) Long id) {
        return super.getById(id);
    }

    @Override
    public List<Student> list(ListQuery query) {
        LambdaQueryWrapper<Student> wrapper = Wrappers.lambdaQuery();
        Page<Student> page = this.page(new Page<>(query.getPage(), query.getSize()), wrapper);
        return page.getRecords();
    }

    /**
     * 新增
     *
     * @param student 要新增的实体信息
     * @return 新增后的实体信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Student insert(@Valid Student student) {
        save(student);
        return student;
    }

    /**
     * 更新
     *
     * @param student 要更新的实体信息
     * @return 数据库中是否有实体被跟新了
     */
    @Override
    public boolean update(@Valid Student student) {
        return updateById(student);
    }

    /**
     * 根据 id 删除
     *
     * @param id 主键
     * @return 数据库中是否有实体被删除了
     */
    @Override
    public boolean remove(@NotNull @Min(1) Long id) {
        return removeById(id);
    }
}
